/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddTask.java
 *
 * Created on 02/06/2010, 22:11:09
 */

package gui;

import exception.RepositoryException;
import base.Task;
import base.UserPreferences;
import base.WeekDays;
import exception.NoRowSelectedException;
import exception.TaskExistsException;
import exception.TaskNotFoundException;
import facade.Facade;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author tacio
 */
public class JFeedsUMain extends javax.swing.JFrame {

    private Facade facade;
    private UserPreferences preferences;
    private DefaultTableModel taskTableModel;
    private DefaultTableModel downloadTableModel;

    /** Creates new form AddTask */
    public JFeedsUMain() {
        try {
            facade = Facade.getFacade();
            preferences = facade.getUserPreferences();
        } catch (RepositoryException ex) {
            Logger.getLogger(JFeedsUMain.class.getName()).log(Level.SEVERE, null, ex);
        }
        initComponents();
        taskTableModel = (DefaultTableModel) jTableTasks.getModel();
        downloadTableModel = (DefaultTableModel) jTableDownloadList.getModel();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroupRSSFeed = new javax.swing.ButtonGroup();
        jTabbedPaneMain = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabelSearchString = new javax.swing.JLabel();
        btAddTask = new javax.swing.JButton();
        jTextFieldSearchString = new javax.swing.JTextField();
        jLabelTaskName = new javax.swing.JLabel();
        jTextFieldTaskName = new javax.swing.JTextField();
        jPanel5 = new javax.swing.JPanel();
        jLabelChooseRssFeeds = new javax.swing.JLabel();
        jRadiobuttonEzRSSFeed = new javax.swing.JRadioButton();
        jLabelShowSchedule = new javax.swing.JLabel();
        jCheckBoxSundays = new javax.swing.JCheckBox();
        jCheckBoxTuesdays = new javax.swing.JCheckBox();
        jCheckBoxThursdays = new javax.swing.JCheckBox();
        jCheckBoxMondays = new javax.swing.JCheckBox();
        jCheckBoxFridays = new javax.swing.JCheckBox();
        jCheckBoxWednesdays = new javax.swing.JCheckBox();
        jCheckBoxSaturdays = new javax.swing.JCheckBox();
        jPanel3 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jButtonEditTask = new javax.swing.JButton();
        jButtonRemoveTask = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableTasks = new javax.swing.JTable();
        jPanel6 = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTableDownloadList = new javax.swing.JTable();
        jPanel8 = new javax.swing.JPanel();
        jPanel9 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();
        jLayeredPanePreferences = new javax.swing.JLayeredPane();
        jPanelTaskPooler = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldRepeatInterval = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jButtonSaveTaskPoolerPreferences = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem3 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("JFeeds-U Control Panel");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jTabbedPaneMain.setInheritsPopupMenu(true);

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabelSearchString.setText("Search String");
        jLabelSearchString.setToolTipText("For EZTV usually it's the show's name");

        btAddTask.setText("Add Task");
        btAddTask.setToolTipText("Click to add new task");
        btAddTask.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAddTaskActionPerformed(evt);
            }
        });

        jTextFieldSearchString.setToolTipText("This will be the actual search string.");

        jLabelTaskName.setText("Task Name");

        jTextFieldTaskName.setToolTipText("Ex: House MD Season 06");

        jPanel5.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabelChooseRssFeeds.setText("Choose RSS Feed");
        jLabelChooseRssFeeds.setAlignmentY(0.0F);

        buttonGroupRSSFeed.add(jRadiobuttonEzRSSFeed);
        jRadiobuttonEzRSSFeed.setSelected(true);
        jRadiobuttonEzRSSFeed.setText("http://www.ezrss.it/feed");
        jRadiobuttonEzRSSFeed.setToolTipText("<html>EZRSS, the RSS feed for EZTV");
        jRadiobuttonEzRSSFeed.setActionCommand("<html>http://eztv.it/feed");
        jRadiobuttonEzRSSFeed.setAlignmentY(0.0F);
        jRadiobuttonEzRSSFeed.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRadiobuttonEzRSSFeed)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabelChooseRssFeeds)))
                .addContainerGap(79, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addComponent(jLabelChooseRssFeeds)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadiobuttonEzRSSFeed)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabelShowSchedule.setText("Show Schedule (airs on)");
        jLabelShowSchedule.setToolTipText("For EZTV usually it's the show's name");

        jCheckBoxSundays.setForeground(new java.awt.Color(255, 0, 0));
        jCheckBoxSundays.setText("sundays");

        jCheckBoxTuesdays.setText("tuesdays");

        jCheckBoxThursdays.setText("thurdays");

        jCheckBoxMondays.setText("mondays");

        jCheckBoxFridays.setText("fridays");

        jCheckBoxWednesdays.setText("wednesdays");

        jCheckBoxSaturdays.setForeground(new java.awt.Color(255, 0, 0));
        jCheckBoxSaturdays.setText("saturdays");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jLabelTaskName)
                                .addComponent(jLabelSearchString)
                                .addComponent(jLabelShowSchedule)
                                .addComponent(jTextFieldTaskName, javax.swing.GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE)
                                .addComponent(jTextFieldSearchString))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jCheckBoxFridays)
                                .addGap(42, 42, 42)
                                .addComponent(jCheckBoxSaturdays))
                            .addComponent(jCheckBoxSundays))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 76, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btAddTask)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jCheckBoxWednesdays)
                            .addComponent(jCheckBoxMondays))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jCheckBoxThursdays)
                            .addComponent(jCheckBoxTuesdays))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelTaskName)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btAddTask))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jTextFieldTaskName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabelSearchString)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldSearchString, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabelShowSchedule)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jCheckBoxMondays)
                            .addComponent(jCheckBoxTuesdays))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jCheckBoxWednesdays)
                            .addComponent(jCheckBoxThursdays))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jCheckBoxFridays)
                            .addComponent(jCheckBoxSaturdays))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCheckBoxSundays)))
                .addGap(131, 131, 131))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPaneMain.addTab("Add Tasks", jPanel1);

        jPanel4.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jButtonEditTask.setText("Edit");
        jButtonEditTask.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEditTaskActionPerformed(evt);
            }
        });

        jButtonRemoveTask.setText("Remove");
        jButtonRemoveTask.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRemoveTaskActionPerformed(evt);
            }
        });

        jTableTasks.setAutoCreateRowSorter(true);
        jTableTasks.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Task Name", "Search String", "RSS Feed", "Schedule"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTableTasks);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                        .addComponent(jButtonEditTask)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonRemoveTask))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 598, Short.MAX_VALUE))
                .addContainerGap())
        );

        jPanel4Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jButtonEditTask, jButtonRemoveTask});

        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonRemoveTask)
                    .addComponent(jButtonEditTask))
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPaneMain.addTab("Task List", jPanel3);

        jPanel7.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jScrollPane3.setPreferredSize(new java.awt.Dimension(452, 402));

        jTableDownloadList.setAutoCreateRowSorter(true);
        jTableDownloadList.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Task Name", "Download Date", "Torrent Name"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(jTableDownloadList);

        jScrollPane3.setViewportView(jScrollPane2);

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 598, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 212, Short.MAX_VALUE)
                .addContainerGap(58, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPaneMain.addTab("Downloaded Torrents", jPanel6);

        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("General");
        javax.swing.tree.DefaultMutableTreeNode treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("TaskPooler Service");
        treeNode1.add(treeNode2);
        jTree1.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        jScrollPane4.setViewportView(jTree1);

        jLayeredPanePreferences.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setText("Run TaskPooler every");

        jTextFieldRepeatInterval.setHorizontalAlignment(javax.swing.JTextField.CENTER);

        jLabel2.setText("minutes.");

        jLabel3.setBackground(new java.awt.Color(255, 255, 255));
        jLabel3.setFont(new java.awt.Font("DejaVu Sans", 1, 18));
        jLabel3.setText("General > Task Pooler Service");

        jButtonSaveTaskPoolerPreferences.setText("Save");
        jButtonSaveTaskPoolerPreferences.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSaveTaskPoolerPreferencesActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelTaskPoolerLayout = new javax.swing.GroupLayout(jPanelTaskPooler);
        jPanelTaskPooler.setLayout(jPanelTaskPoolerLayout);
        jPanelTaskPoolerLayout.setHorizontalGroup(
            jPanelTaskPoolerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTaskPoolerLayout.createSequentialGroup()
                .addGroup(jPanelTaskPoolerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addGroup(jPanelTaskPoolerLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)
                        .addGap(4, 4, 4)
                        .addComponent(jTextFieldRepeatInterval, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(3, 3, 3)
                        .addComponent(jLabel2)))
                .addContainerGap(116, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelTaskPoolerLayout.createSequentialGroup()
                .addContainerGap(364, Short.MAX_VALUE)
                .addComponent(jButtonSaveTaskPoolerPreferences)
                .addContainerGap())
        );
        jPanelTaskPoolerLayout.setVerticalGroup(
            jPanelTaskPoolerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelTaskPoolerLayout.createSequentialGroup()
                .addComponent(jLabel3)
                .addGap(18, 18, 18)
                .addGroup(jPanelTaskPoolerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldRepeatInterval, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 152, Short.MAX_VALUE)
                .addComponent(jButtonSaveTaskPoolerPreferences)
                .addContainerGap())
        );

        jPanelTaskPooler.setBounds(10, 10, 420, 260);
        jLayeredPanePreferences.add(jPanelTaskPooler, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout jPanel9Layout = new javax.swing.GroupLayout(jPanel9);
        jPanel9.setLayout(jPanel9Layout);
        jPanel9Layout.setHorizontalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel9Layout.createSequentialGroup()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLayeredPanePreferences, javax.swing.GroupLayout.DEFAULT_SIZE, 446, Short.MAX_VALUE))
        );
        jPanel9Layout.setVerticalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE)
            .addComponent(jLayeredPanePreferences, javax.swing.GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE)
        );

        fillPreferencesPanels();

        javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8);
        jPanel8.setLayout(jPanel8Layout);
        jPanel8Layout.setHorizontalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel8Layout.setVerticalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPaneMain.addTab("Preferences", jPanel8);

        jMenu1.setText("File");

        jMenuItem4.setText("Exit");
        jMenu1.add(jMenuItem4);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Help");

        jMenuItem3.setText("About JFeeds-U");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem3);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPaneMain, javax.swing.GroupLayout.DEFAULT_SIZE, 662, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPaneMain, javax.swing.GroupLayout.PREFERRED_SIZE, 353, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btAddTaskActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAddTaskActionPerformed

        //Verifica se os campos foram escolhidos e pelo menos um dia da semana foi marcado
        String url = "http://www.ezrss.it/feed";
        if (!jTextFieldTaskName.getText().isEmpty() && 
                !jTextFieldSearchString.getText().isEmpty() &&
                jRadiobuttonEzRSSFeed.isSelected() &&
                (jCheckBoxFridays.isSelected() |
                jCheckBoxMondays.isSelected() |
                jCheckBoxSaturdays.isSelected() |
                jCheckBoxSundays.isSelected() |
                jCheckBoxThursdays.isSelected() |
                jCheckBoxTuesdays.isSelected() |
                jCheckBoxWednesdays.isSelected()))
                {
            Task task = new Task(jTextFieldTaskName.getText().trim(),jTextFieldSearchString.getText().trim(),url);

            ArrayList<WeekDays> schedule = new ArrayList<WeekDays>();
            if (jCheckBoxFridays.isSelected())
                schedule.add(WeekDays.FRIDAY);
            if (jCheckBoxMondays.isSelected())
                schedule.add(WeekDays.MONDAY);
            if (jCheckBoxSaturdays.isSelected())
                schedule.add(WeekDays.SATURDAY);
            if (jCheckBoxSundays.isSelected())
                schedule.add(WeekDays.SUNDAY);
            if (jCheckBoxThursdays.isSelected())
                schedule.add(WeekDays.THURSDAY);
            if (jCheckBoxTuesdays.isSelected())
                schedule.add(WeekDays.TUESDAY);
            if (jCheckBoxWednesdays.isSelected())
                schedule.add(WeekDays.WEDNESDAY);

            task.setSchedule(schedule);

            try {
                facade.addTask(task);
                updateTaskTable();
                JOptionPane.showMessageDialog(this, "Task added with success!");
                jTextFieldTaskName.setText("");
                jTextFieldSearchString.setText("");
            } catch (RepositoryException ex) {
                JOptionPane.showMessageDialog(this, ex.getMessage());
            } catch (TaskExistsException ex) {
                JOptionPane.showMessageDialog(this, ex.getMessage());
                jTextFieldTaskName.selectAll();
            }
        } else {
            JOptionPane.showMessageDialog(this, "Please fill all the fields.");
        }
}//GEN-LAST:event_btAddTaskActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
        fillTaskTable();
        fillDownloadTable();
    }//GEN-LAST:event_formWindowOpened

    private void jButtonRemoveTaskActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRemoveTaskActionPerformed
        try {
            // TODO add your handling code here:
            removeSelectedTask();
        } catch (NoRowSelectedException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }//GEN-LAST:event_jButtonRemoveTaskActionPerformed

    private void jButtonEditTaskActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEditTaskActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButtonEditTaskActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        AboutJFeedsU aboutWindow = new AboutJFeedsU(this, true);
                    aboutWindow.setVisible(true);
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jButtonSaveTaskPoolerPreferencesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSaveTaskPoolerPreferencesActionPerformed
        preferences.setTaskPoolerRepeatInterval(new Integer(jTextFieldRepeatInterval.getText()));
        try {
            facade.setUserPreferences(preferences);
        } catch (RepositoryException ex) {
            Logger.getLogger(JFeedsUMain.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButtonSaveTaskPoolerPreferencesActionPerformed

    private void updateTaskTable() {
        try {
           
            int i = taskTableModel.getRowCount();
            while(taskTableModel.getRowCount() > 0){
                taskTableModel.removeRow(i - 1);
                i--;
            }
            
            ArrayList<String[]> taskArray = facade.getTaskList();
            if (!taskArray.isEmpty()) {
                for (String[] taskRow : taskArray) {
                    taskTableModel.addRow(taskRow);
                }
            }
        } catch (RepositoryException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }

    }

    private void fillTaskTable(){
         try {            
            ArrayList<String[]> taskArray = facade.getTaskList();
            if (!taskArray.isEmpty()) {
                for (String[] taskRow : taskArray) {
                    taskTableModel.addRow(taskRow);
                }
            }
        } catch (RepositoryException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }

     private void fillDownloadTable(){
         try {
            ArrayList<String[]> downloadArray = facade.getDownloadList();
            if (!downloadArray.isEmpty()) {
                for (String[] downloadRow : downloadArray) {
                    downloadTableModel.addRow(downloadRow);
                }
            }
        } catch (RepositoryException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }

    private void removeSelectedTask() throws NoRowSelectedException {
        try {

            int[] selectedRows = jTableTasks.getSelectedRows();
            int selectedRowsCount = selectedRows.length;
            
            if (selectedRowsCount != 0) {

                ArrayList<String> taskNames = new ArrayList<String>();
                for (int i : selectedRows) {
                    taskNames.add((String) jTableTasks.getValueAt(i, 0));                    
                }
                
                String removedTasks = new String();
                for (String taskString : taskNames) {
                    removedTasks += taskString + "\n";
                }

                for (String task : taskNames) {
                    facade.removeTask(task);
                }

                while (selectedRowsCount > 0) {
                    taskTableModel.removeRow(selectedRows[selectedRowsCount - 1]);
                    selectedRows = jTableTasks.getSelectedRows();
                    selectedRowsCount = selectedRows.length;
                }
                
                if (taskNames.size() == 1) {
                    JOptionPane.showMessageDialog(this, "The following task was removed:" + "\n\n" + removedTasks + "\n");
                } else {
                    JOptionPane.showMessageDialog(this, "The following tasks were removed:" + "\n\n" + removedTasks);
                }
                
            } else {
                throw new NoRowSelectedException();
            }

        } catch (RepositoryException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        } catch (TaskNotFoundException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }

    private void fillPreferencesPanels(){
        try {
            preferences = facade.getUserPreferences();
            jTextFieldRepeatInterval.setText(preferences.getTaskPoolerRepeatInterval().toString());
        } catch (RepositoryException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new JFeedsUMain().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAddTask;
    private javax.swing.ButtonGroup buttonGroupRSSFeed;
    private javax.swing.JButton jButtonEditTask;
    private javax.swing.JButton jButtonRemoveTask;
    private javax.swing.JButton jButtonSaveTaskPoolerPreferences;
    private javax.swing.JCheckBox jCheckBoxFridays;
    private javax.swing.JCheckBox jCheckBoxMondays;
    private javax.swing.JCheckBox jCheckBoxSaturdays;
    private javax.swing.JCheckBox jCheckBoxSundays;
    private javax.swing.JCheckBox jCheckBoxThursdays;
    private javax.swing.JCheckBox jCheckBoxTuesdays;
    private javax.swing.JCheckBox jCheckBoxWednesdays;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabelChooseRssFeeds;
    private javax.swing.JLabel jLabelSearchString;
    private javax.swing.JLabel jLabelShowSchedule;
    private javax.swing.JLabel jLabelTaskName;
    private javax.swing.JLayeredPane jLayeredPanePreferences;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JPanel jPanelTaskPooler;
    private javax.swing.JRadioButton jRadiobuttonEzRSSFeed;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTabbedPane jTabbedPaneMain;
    private javax.swing.JTable jTableDownloadList;
    private javax.swing.JTable jTableTasks;
    private javax.swing.JTextField jTextFieldRepeatInterval;
    private javax.swing.JTextField jTextFieldSearchString;
    private javax.swing.JTextField jTextFieldTaskName;
    private javax.swing.JTree jTree1;
    // End of variables declaration//GEN-END:variables

}
